<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-10-17
 * Time: 14:41:07
 * Info:
 */

namespace plugin\huiblog\app\controller\module;

use support\Request;
use support\Cache;
use plugin\huiblog\app\model\NavGroup as NavGroupModel;
use plugin\huiblog\app\model\Nav as NavModel;
use plugin\huiblog\app\common\CacheClear;

class NavController
{

    /**
     * 导航组列表
     *
     * @param Request $request
     *
     */
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $limit = (int)$request->input('limit', 15);
            $page  = (int)$request->input('page', 1);
            $key   = $request->input('key');
            $first = ($page - 1) * $limit;

            $where = function ($query) use ($key) {
                if ( ! empty($key['name'])) {
                    $query->whereLike('name', "%".$key['name']."%");
                }
                if ( ! empty($key['src'])) {
                    $query->whereLike('src', "%".$key['src']."%");
                }
            };
            $count = NavGroupModel::where($where)->count();
            $data  = NavGroupModel::where($where)->limit($first, $limit)->select()->toArray();

            return json(['code' => 0, 'count' => $count, 'data' => $data, 'msg' => 'ok']);
        }

        return view('admin/nav_group/index');
    }

    public function group_add(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '分组名称不能为空']);
            }
            if (empty($param['name'])) {
                return json(['code' => 0, 'msg' => '标识不能为空']);
            }
            NavGroupModel::create($param);

            return json(['code' => 200, 'msg' => '操作成功']);
        }

        return view('admin/nav_group/add');
    }

    public function group_edit(Request $request)
    {
        $id = $request->get('id');
        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['id'])) {
                return json(['code' => 0, 'msg' => '参数错误']);
            }
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '分组名称不能为空']);
            }
            if (empty($param['name'])) {
                return json(['code' => 0, 'msg' => '标识不能为空']);
            }
            NavGroupModel::update($param, ['id' => $param['id']]);

            return json(['code' => 200, 'msg' => '操作成功']);
        }
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        $data = NavGroupModel::find($id);
        if (empty($data)) {
            return json(['code' => 0, 'msg' => '获取数据失败']);
        }

        return view('admin/nav_group/edit', ['data' => $data]);
    }

    public function group_delete(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        //判断分类下是否存在数据，如果有禁止删除
        $slideCount = NavModel::where('group_id', $id)->count();
        if ($slideCount) {
            return json(['code' => 0, 'msg' => '该组下有导航数据，无法删除']);
        }
        NavGroupModel::where('id', $id)->delete();

        return json(['code' => 200, 'msg' => '删除成功']);
    }

    public function nav_index(Request $request)
    {
        if ($request->isAjax()) {
            $groupId = $request->get('group_id');
            $data    = NavModel::where('group_id', $groupId)->select()->toArray();

            return json(['code' => 0, 'data' => $data, 'msg' => 'ok']);
        }
        $groupId = $request->get('group_id');
        if (empty($groupId)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }

        return view('admin/nav/index', ['group_id' => $groupId]);
    }

    public function nav_add(Request $request)
    {
        $id       = $request->get('id', '');
        $groupId  = $request->get('group_id');
        $navGroup = (new NavModel())->getPidNavList();
        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '分类名称不能为空']);
            }
            NavModel::create($param);
            CacheClear::clearBlogNavs($groupId);

            return json(['code' => 200, 'msg' => '操作成功']);
        }

        return view('admin/nav/add', ['id' => $id, 'nav_group' => $navGroup, 'group_id' => $groupId]);
    }

    public function nav_edit(Request $request)
    {
        $id       = $request->get('id');
        $groupId  = $request->get('group_id');
        $navGroup = (new NavModel())->getPidNavList();

        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['id'])) {
                return json(['code' => 0, 'msg' => '参数错误']);
            }
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '分类名称不能为空']);
            }
            NavModel::update($param, ['id' => $param['id']]);
            CacheClear::clearBlogNavs($param['group_id']);


            return json(['code' => 200, 'msg' => '操作成功']);
        }
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        $data = NavModel::find($id);
        if (empty($data)) {
            return json(['code' => 0, 'msg' => '获取数据失败']);
        }

        return view('admin/nav/edit', ['data' => $data, 'nav_group' => $navGroup, 'group_id' => $groupId]);
    }

    public function nav_delete(Request $request)
    {
        $id      = $request->get('id');
        $groupId = $request->get('group_id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        //判断是否有子级数据
        $sonData = NavModel::where(['pid' => $id])->count();
        if ($sonData) {
            return json(['code' => 0, 'msg' => '该导航下有子数据，无法删除']);
        }
        NavModel::where('id', $id)->delete();
        CacheClear::clearBlogNavs($groupId);

        return json(['code' => 200, 'msg' => '删除成功']);
    }

    public function nav_sort(Request $request)
    {
        $id      = $request->post('id');
        $sort    = $request->post('val');
        $groupId = $request->post('group_id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        NavModel::update(['sort' => $sort], ['id' => $id]);
        CacheClear::clearBlogNavs($groupId);

        return json(['code' => 200, 'msg' => '操作成功']);
    }

}
